
#ifndef _GAMESCREEN
#define _GAMESCREEN

#include <CEGUI/CEGUI.h>
#include <OIS/OIS.h>
#include <OgreCEGUIRenderer.h>
#include <list>

#define CAMERASTEP 10.0

class GameScreen : public Screen
{
public:
	GameScreen(){}
	GameScreen(CEString  layoutFile):Screen(layoutFile)
	{
		mSceneMgr = Ogre::Root::getSingletonPtr()->getSceneManager("Default SceneManager");

		mCamera = mSceneMgr->getCamera("Camera");

		mSceneMgr->setAmbientLight(Ogre::ColourValue(0.2, 0.2, 0.2));
		Ogre::Light *light = mSceneMgr->createLight("Light");
		
		//mSceneMgr->setSkyDome(true, "Examples/CloudySky", 5, 8);

		Ogre::Entity* ogreHead = mSceneMgr->createEntity("Head", "ogrehead.mesh");
		Ogre::SceneNode* headNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(Ogre::Vector3(0, 0, -300));
		headNode->attachObject(ogreHead);

		Ogre::RenderTexture *tex= Ogre::Root::getSingletonPtr()->getTextureManager()->createManual
                                  ("MiniMap", "Default", Ogre::TEX_TYPE_2D, 512, 512, 0, Ogre::PF_R8G8B8, Ogre::TU_RENDERTARGET)
                                   ->getBuffer()->getRenderTarget();

		mMiniMapCamera = mSceneMgr->createCamera("MiniMapCam");
		Ogre::Vector3 pos = mCamera->getPosition();
		mMiniMapCamera->setPosition(pos.x, pos.y+250, pos.z);
		mMiniMapCamera->lookAt(pos);

		Ogre::Viewport *v = tex->addViewport(mMiniMapCamera);
		v->setOverlaysEnabled(false);
		v->setClearEveryFrame(true);
		v->setBackgroundColour(Ogre::ColourValue::Black);

		CEGUI::Texture *cTex = getGUIManager()->getRenderer()->createTexture((CEGUI::utf8*)"MiniMap");

		CEGUI::Imageset *imageSet = CEGUI::ImagesetManager::getSingleton().createImageset((CEGUI::utf8*)"MiniMapImageset", cTex);
		imageSet->defineImage((CEGUI::utf8*)"MiniMapImage", 
           CEGUI::Point(0.0f, 0.0f),
           CEGUI::Size(cTex->getWidth(), cTex->getHeight()),
           CEGUI::Point(0.0f,0.0f));

		CEGUI::Window *wnd = getWindowManager()->getWindow((CEGUI::utf8*)"Sapphie/InGame/MiniDisplay/Image");
		wnd->setProperty("Image", CEGUI::PropertyHelper::imageToString(&imageSet->getImage((CEGUI::utf8*)"MiniMapImage")));

		mMiniMapSize.d_offset=0.0;
		mMiniMapSize.d_scale=2.0;
		
		wnd = getWindowManager()->getWindow((CEGUI::utf8*)"Sapphie/InGame/MiniDisplay");
		mMiniMapPos.d_offset=0.0;
		mMiniMapPos.d_scale=wnd->getSize().d_x.d_scale;
		


	}
	// Events
	void subscribeEvents()
	{
		CEGUI::Event::Connection connect;

		CEGUI::Window *wnd = getWindowManager()->getWindow((CEGUI::utf8*)"Sapphie/InGame/MiniDisplay/Image");
		connect = wnd->subscribeEvent(CEGUI::Window::EventMouseDoubleClick,CEGUI::Event::Subscriber(&GameScreen::resizeMiniMap, this));
		mEventConnections.push_front(connect);
	}
	bool resizeMiniMap(const CEGUI::EventArgs & arg)
	{
		CEGUI::Window *wnd = getWindowManager()->getWindow((CEGUI::utf8*)"Sapphie/InGame/MiniDisplay");
		
		wnd->setSize(CEGUI::UVector2(wnd->getSize().d_x * mMiniMapSize,wnd->getSize().d_y * mMiniMapSize));
		wnd->setPosition(CEGUI::UVector2(wnd->getPosition().d_x - mMiniMapPos,wnd->getPosition().d_y));
		mMiniMapSize.d_scale = 1/mMiniMapSize.d_scale;
		mMiniMapPos.d_scale = mMiniMapPos.d_scale* -1.0;

		return true;
	}
	// MouseListener
    bool mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id){return true;}
    // KeyListener
    bool keyPressed(const OIS::KeyEvent &arg)
	{
		switch (arg.key)
		{
		case OIS::KC_UP:
			mCamera->move(Ogre::Vector3(0,0,-CAMERASTEP));
			mMiniMapCamera->move(Ogre::Vector3(0,0,-CAMERASTEP));
			break;
		case OIS::KC_DOWN:
			mCamera->move(Ogre::Vector3(0,0,CAMERASTEP));
			mMiniMapCamera->move(Ogre::Vector3(0,0,CAMERASTEP));
			break;
		}
		if (arg.key == OIS::KC_ESCAPE)
			mContinue=false;
		return true;
	}
	
	
private:
	Ogre::SceneManager *mSceneMgr;
	Ogre::Camera *mCamera;
	Ogre::Camera *mMiniMapCamera;
	CEGUI::UDim mMiniMapSize;
	CEGUI::UDim mMiniMapPos;
	
};

#endif